<template>
	<view class="box">
		<!-- 头部 -->
		<view class="head flex">
			<view class="input f-w">
				<view class="input-img f-w">
					<image src="https://jw.xzsw2021.com/static/index/icon_32@2x.png" mode=""></image>
				</view>
				<input type="text" v-model.trim="keyword" placeholder="请输入商品搜索"  @confirm="search" confirm-type="search">
				<view class="input-btn" @click="search">搜索</view>
			</view>
			<view class="lingquan" @click="goQuan">
				<view class="head-img f-w">
					<image src="https://jw.xzsw2021.com/static/client/icon_75@2x.png" mode=""></image>
				</view>
				<view>领券</view>
			</view>
		</view>
		<!-- 店铺主体信息 -->
		<view class="content">
			<view class="list" v-if="list.length">
				<view class="card f-w" v-for="(item,index) in list" :key="index" @click="goShopingDetail(item.product_id)"> 
					<!-- 商品图片 -->
					<view class="card-img f-w">
					<image :src="item.image" mode=""></image>
					</view>
					<!-- 商品详细 -->
					<view class="card-right">
						<view class="one f28 bold hide-line">{{item.store_name}}</view>
						<view class="two">月销{{item.sales}}</view>
						<view class="three" v-if="item.seckillActive">
							<view v-if="item.seckillActive.all_pay_count">总秒杀限定{{item.seckillActive.all_pay_count}}份</view>
							<view v-else>秒杀商品</view>
						</view>
						<view class="four fw-bt">
							<!-- 价格 -->
							<view class="four-left f-w">
								<view class="price1 f28 bold">￥{{item.price}}</view>
								<view class="price2" >￥{{item.productAttrValue.attrValue[0].ot_price}}</view>
							</view>
							<!-- 购物-->
							<!-- 购物-->
							<my-numberBox :info="item" @cartDel='cartDel(index)' @cartAdd='cartAdd(index)'></my-numberBox>
						</view>
					</view>
				</view>
			</view>
			<!-- 空 -->
			<view class="none" v-else>
				<view class="nimg f-w">
					<image src="https://jw.xzsw2021.com/static/client/icon_66@2x.png" mode=""></image>
				</view>
				<view class="ndesc">抱歉，未能找到您搜索的商品</view>
			</view>
		</view>
		<coupons ref="coupons" :quan_list="couponlist" @getid='getid'></coupons>
		<!-- 购物车 -->
		<cart></cart>
	</view>
</template>

<script>
	import cart from '@/components/cart.vue'
	export default {
		components: {
			cart
		},
		data() {
			return {
				couponlist:[],
				page: 1,
				id: '',
				num: 0, //
				serverUrl: '', //服务器地址
				keyword: '', //
				list: [], //商品列表
			}
		},
		onReachBottom() {
			this.getcate3(this.page + 1)
		},
		onLoad(e) {
			this.id = e.id
			this.keyword=e.keyword
			uni.setNavigationBarTitle({
				title: e.shopname
			})
			this.getcoupon()
			this.search()
		},
		methods: {
			// 购物车减去
			cartDel(e){
				let list=this.list
				let item=list[e]
				let data = {
					cart_num: 1,
					is_new: 0,
					product_attr_unique:item.productAttrValue.attrValue[0].unique,
					product_id:item.product_id,
					product_type:item.product_type
				}
				uni.showLoading({
					mask:true
				})
				this.$request('user/cart/create',data).then(res=>{
					if(res.status==200){
						uni.showToast({
							title: "添加成功"
						})
						list[e].cartnum--
						this.list=list
						this.$forceUpdate()
						
					}
				})
			},
				// 购物车加
			cartAdd(e){
				let list=this.list
				let item=list[e]
				let data = {
					cart_num: 1,
					is_new: 0,
					product_attr_unique:item.productAttrValue.attrValue[0].unique,
					product_id:item.product_id,
					product_type:item.product_type
				}
				uni.showLoading({
					mask:true
				})
				this.$request('user/cart/create',data).then(res=>{
					if(res.status==200){
						uni.showToast({
							title: "添加成功"
						})
						list[e].cartnum++
						this.list=list
						this.$forceUpdate()
						
					}
				})
			},
			getid(e){
				uni.showLoading({
					mask:true
				})
				this.$request('coupon/receive/' +e).then(res => {
					if(res.status==200){
						uni.hideLoading()
					}
					 this.getcoupon()
				})
			},
			// 此商店的优惠券
			getcoupon(){
				this.$request('coupon/store/' + this.id, {}, 'GET').then(res => {
					res.data.forEach(item=>{
						item.look=false
					})
					this.couponlist=res.data
				})
			},
			// 去商品详情
			goShopingDetail(id) {
				uni.navigateTo({
					url: '/pages/client/shop/shop-detail/shop-detail?id=' + id
				})
			},
			// 搜索
			search() {
				this.page = 1
				this.getcate3(this.page)
			},
			// 获取店铺的分类的商品
			getcate3(e) {
				let data = {
					page: e,
					keyword: this.keyword,
					limit: 10
				}
				uni.showLoading({
					mask: true
				})
				this.$request('product/spu/merchant/' + this.id, data, 'GET').then(res => {
					res.data.list.forEach(item=>{
						item.cartnum=0
					})
					uni.hideLoading()
					if (e == 1) {
						this.list = res.data.list
					} else {
						if (res.data.list.length == 0) {
							uni.showToast({
								title: "暂无更多",
								icon: "none"
							})
							return
						}
						let list = this.list
						list = [...list, ...res.data.list]
						this.list = list
						this.page = e
					}
				})
			},
			// 领券
			goQuan() {
				this.$refs.coupons.cpopOpen()
				console.log('领券');
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		.myimg {
			width: 100%;
			height: 100%;
		}

		.color {
			color: #109C7A;
		}

		.tag1 {
			margin-right: 10rpx;
			color: #fff;
			font-size: 22rpx;
			padding: 2rpx 8rpx;
			background: linear-gradient(0deg, #FF3B38 0%, #FA5F3D 100%);
			border-radius: 4rpx;
		}

		.tag2 {
			margin-right: 10rpx;
			color: #F83535;
			font-size: 22rpx;
			padding: 2rpx 8rpx;
			background: #FFF2F2;
			border: 1rpx solid #FFCDCD;
			border-radius: 4rpx;
		}

		.head {
			height: 480rpx;
			padding: 26rpx 30rpx 0;
			background: #109C7A;
			background: linear-gradient(180deg, #109C7A 0%, rgba(16, 156, 122, 0) 100%);

			.input {
				width: 580rpx;
				height: 68rpx;
				background: #F3F3F3;
				border-radius: 34rpx;

				.input-img {
					margin: 0 14rpx 0 26rpx;
					width: 30rpx;
					height: 30rpx;

					image {
						@extend .myimg
					}
				}

				input {
					width: 386rpx;
					font-size: 26rpx;
				}

				.input-btn {
					width: 120rpx;
					height: 60rpx;
					line-height: 60rpx;
					text-align: center;
					font-size: 26rpx;
					color: #fff;
					background: linear-gradient(64deg, #2BCCA5, #22B591);
					border-radius: 30rpx;
				}
			}

			.lingquan {
				margin-left: 40rpx;
				height: 68rpx;
				font-size: 26rpx;
				text-align: center;
				color: #fff;

				.head-img {
					margin: 4rpx auto 0;
					width: 37rpx;
					height: 31rpx;
					margin-bottom: 4rpx;

					image {
						@extend .myimg
					}
				}
			}
		}

		.content {
			position: fixed;
			left: 0;
			padding-bottom: 160rpx;
			top: 134rpx;
			width: 750rpx;
			height: 1600rpx;
			background: #FFFFFF;
			border-radius: 30rpx 30rpx 0 0;
			z-index: 11;
			box-sizing: border-box;
			overflow-y: auto;

			.list {
				padding: 0 30rpx;

				.card {
					padding: 30rpx 0;
					border-bottom: 1rpx solid #eee;

					.card-img {
						flex-shrink: 0;
						width: 190rpx;
						height: 190rpx;

						image {
							@extend .myimg;
							border-radius: 12rpx;
						}
					}

					.card-right {
						position: relative;
						flex: 1;
						margin-left: 15rpx;
						min-height: 190rpx;
						overflow: hidden;

						.one {
							width: 356rpx;
						}

						.two {
							margin: 10rpx 0;
							color: #666;
							font-size: 22rpx;
						}

						.three {
							width: 120rpx;
							height: 32rpx;
							line-height: 32rpx;
							text-align: center;
							font-size: 20rpx;
                            display: inline-block;
							color: #AF1919;
							background: rgba(16, 156, 122, 0);
							border: 1rpx solid #AF1919;
						}

						.four {
							position: absolute;
							right: 0;
							bottom: 0;
							width: 486rpx;
							height: 40rpx;

							.price1 {
								color: #F83535;
							}

							.price2 {
								margin-left: 10rpx;
								color: #999;
								font-size: 26rpx;
								text-decoration: line-through;
							}

							.cart1 {
								input {
									width: 60rpx;
									font-weight: 600;
									text-align: center;
								}

								.cart1-jian,
								.cart1-add {
									width: 40rpx;
									height: 40rpx;

									image {
										@extend .myimg;
									}
								}
							}
						}
					}
				}
			}

			.none {
				text-align: center;

				.nimg {
					margin: 95rpx auto 25rpx;
					width: 366rpx;
					height: 330rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.ndesc {
					color: #666;
					font-size: 30rpx;
				}
			}
		}
	}
</style>
